package com.axun.ckz.yytmanager.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by ckz on 2018/5/2.
 */

public class UserShare {
    private static UserShare instance;
    private Context context;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private UserShare(Context context){
        this.context = context;
        preferences = context.getSharedPreferences("USER",Context.MODE_PRIVATE);
        editor = preferences.edit();
    }

    public static UserShare getInstance(Context context){
        if (instance == null){
            synchronized (UserShare.class){
                if (instance == null){
                    instance = new UserShare(context);
                }
            }
        }
        return instance;
    }

    public void saveUser(String userId,String name,int accountId,String sessionId){
        editor.putString("userId",userId);
        editor.putString("userName",name);
        editor.putInt("accountId",accountId);
        editor.putString("sessionId",sessionId);
        editor.commit();
    }

    public void save(String key,Object value){
        if (value instanceof String){
            editor.putString(key, (String) value);
        }else if (value instanceof Integer){
            editor.putInt(key, (Integer) value);
        }else if (value instanceof Long){
            editor.putLong(key, (Long) value);
        }else if (value instanceof Float){
            editor.putFloat(key, (Float) value);
        }else if (value instanceof Boolean){
            editor.putBoolean(key, (Boolean) value);
        }
        editor.commit();
    }

    public void clearUser(){
        editor.clear();
    }

    public String getUserId(){
        return preferences.getString("userId","");
    }

    public String getKey(){
        return preferences.getString("key","");
    }

    public int getIntValue(String key){
        return preferences.getInt(key,0);
    }

    public String getStringValue(String key){
        return preferences.getString(key,"");
    }

}
